<?php
include("../conn/conn.php");


if(!$_SESSION["username"]){
	echo json_encode(array('error'=>'2','msg'=>'请先登录'));
	exit;
}

$id=strval($_POST['id']);
$shuliang = strval($_POST['total']);

$sql=mysql_query("select * from tb_shangpin where id='".$id."'",$conn);
$info=mysql_fetch_array($sql);
if($shuliang=="")
$shuliang=1;
if($info[shuliang]<=0){
	echo json_encode(array('error'=>'1','msg'=>'该商品已经售完'));
	exit;
}

$array=explode("@",$_SESSION[producelist]);//把字符串分割为数组
for($i=0;$i<count($array)-1;$i++){
	if($array[$i]==$id){
		echo json_encode(array('error'=>'1','msg'=>'该商品已经在您的购物车中'));
		exit;
	}
}
$_SESSION[producelist]=$_SESSION[producelist].$id."@";
$_SESSION[quatity]=$_SESSION[quatity].$shuliang."@";
$total_price = $info['shichangjia']*$shuliang;

$_SESSION["total"]+=$total_price;
$_SESSION["total"]=number_format($_SESSION["total"],1);

$html = "";
$html ='
<table  border="1" width="196px" style="margin-left:5px;table-layout:fixed;" id="cart_item_'.$info['id'].'" class="order_table">
	<tr>
		<td style="text-align:left" class="order_table_td2">'.$info['mingcheng'].'</td>
		<td class="order_table_td1">
			<span class="ft_b">'.$shuliang.'</span>
		</td>
		<td class="order_table_td3">
			'.number_format($total_price,1).'
		</td>
		<td>
			<a href="javascript:;" onClick="cart_item_delete('.$info['id'].');">
			<img src="kfc_files/delete_icon.gif">
			</a>
		</td>
	</tr>
</table>
';

echo json_encode(array('error'=>'0','msg'=>$html,'total'=>$_SESSION["total"]));
exit;

?>